<?php
/**
 * 分类模型
 *
 * Author:李强
 * Company:上海煦鸣网络科技有限公司
 * Date:2018-01-03
 */

namespace App\Models;


use App\Services\Service\DataService;

class Category extends CommonModel
{
    use \App\Traits\Category;
    //
    protected $table = 'categorys';

    public function param_options()
    {
        $params = [];
        $params['type'] = [
            'video' => '视频分类', 'shop' => '商家分类', 'help' => '帮助分类',
            'article' => '文章分类', 'notice' => '通知分类', 'suggest' => '工单分类'
        ];
        return $params;
    }

    /**
     * 根据分类类型获取列表
     *
     * @param string $type
     * @param int $pid
     * @return mixed
     */
    public static function getListByType($type = 'article', $pid = 0)
    {
        $categorys = self::where(['type' => $type, 'status' => 1])->orderBy('listorder', 'asc')->get();
        $datas = DataService::channelLevel($categorys, $pid, "", 'id', 'parent_id');
        return $datas;
    }

    /**
     * 获取上级信息
     *
     * @return bool
     */
    public function parent()
    {
        $parent = self::find($this->parent_id);
        return is_null($parent) ? false : $parent;
    }

    /**
     * 文章数量
     *
     * @return int
     */
    public function article_number()
    {
        $count = Article::where(['catid' => $this->id, 'status' => 1])->count();
        return $count;
    }
}
